package com.heaven.back.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.heaven.back.WebSocket.WebSocketUtil;
import com.heaven.back.mapper.ActionMapper;
import com.heaven.back.mapper.ChatMapper;
import com.heaven.back.mapper.RefreshMapper;
import com.heaven.back.pojo.Player;
import com.heaven.back.service.RefreshService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * The type Refresh serviceimpl.
 */
@Slf4j
@Service
public class RefreshServiceimpl implements RefreshService {

    @Autowired
    private RefreshMapper refreshMapper;

    @Autowired
    private ActionMapper actionMapper;

    @Autowired
    private ChatMapper chatMapper;

    //给处在这两个房间用户发送更新玩家列表信息
    @Override
    public void updatePlayerList(String leaveRoom, String arriveRoom, Integer id) throws Exception {
        //通过roomname找到id
        ArrayList<Integer> ids = refreshMapper.getidsByRoomName(leaveRoom);
        if (!leaveRoom.equals(arriveRoom)) ids.addAll(refreshMapper.getidsByRoomName(arriveRoom));
        Player e = actionMapper.searchByid(id);
        //id与socket连接uid相对应
        //遍历uid发送请求
        for (int i = 0; i < ids.size(); i++) {
            Player p = actionMapper.searchByid(ids.get(i));
            if (p.isLogin() && p.getId() != id) {
                Map msgMap = new HashMap();
                msgMap.put("type", "updatePlayerList");
                msgMap.put(leaveRoom, e.getName() + " 离开当前房间");
                msgMap.put(arriveRoom, e.getName() + " 进入当前房间");
                log.info(msgMap.toString());
                WebSocketUtil.sendInfo(JSONObject.toJSONString(msgMap), ids.get(i));
            }
        }
    }

    //给处在这个房间的用户发送更新房间物品列表信息
    @Override
    public void updateRoomItem(String roomName, String item, String action, Integer id) throws Exception {
        //通过roomname找到id
        //id与socket连接uid相对应
        //遍历uid发送请求
        ArrayList<Integer> ids = refreshMapper.getidsByRoomName(roomName);
        Player e = actionMapper.searchByid(id);
        //id与socket连接uid相对应
        //遍历uid发送请求
        for (int i = 0; i < ids.size(); i++) {
            Player p = actionMapper.searchByid(ids.get(i));
            if (p.isLogin()) {
                Map msgMap = new HashMap();
                msgMap.put("type", "updateItemList");
                msgMap.put("msg", e.getName() + action + item);
                WebSocketUtil.sendInfo(JSONObject.toJSONString(msgMap), ids.get(i));
            }
        }
    }

    @Override
    public void updatePlayerLeave(String name) throws Exception {
        ArrayList<Integer> ids = chatMapper.getids();
        //id与socket连接uid相对应
        //遍历uid发送请求
        for (int i = 0; i < ids.size(); i++) {
            Player p = actionMapper.searchByid(ids.get(i));
            if (p.isLogin()) {
                Map msgMap = new HashMap();
                msgMap.put("type", "updatePlayerExit");
                msgMap.put("msg", name + " run了!!!");
                WebSocketUtil.sendInfo(JSONObject.toJSONString(msgMap), ids.get(i));
            }
        }

    }
}
